package com.doublefish.doublefishrobot.base;

import android.app.Activity;
import android.app.Application;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.doublefish.doublefishrobot.R;
import com.doublefish.doublefishrobot.utils.AppCacheUtil;
import com.doublefish.doublefishrobot.utils.LocalizationUtils;
import com.doublefish.doublefishrobot.utils.MessageEvent;
import com.doublefish.doublefishrobot.utils.SharedPreferencesUtils;
import com.doublefish.doublefishrobot.utils.ToastUtil;
import com.doublefish.doublefishrobot.utils.keyboard.KeyboardVisibilityObserver;
import com.tencent.bugly.crashreport.CrashReport;

import org.greenrobot.eventbus.EventBus;

/**
 * 作者:dugaofeng on 2024/10/16 11:10
 */
public class DFApplication extends Application implements Application.ActivityLifecycleCallbacks {

    private int activityReferences = 0;
    private boolean isActivityChangingConfigurations = false;

    @Override
    public void onCreate() {
        super.onCreate();

        //初始化偏好设置
        SharedPreferencesUtils.getInstant().init(getApplicationContext());

        //国际化 生成新的context
//        Context context = LocalizationUtils.getInstance().showLanguage(getApplicationContext());
        Context context = LocalizationUtils.getInstance().showLanguage(this);
        //配置全局的context
        //用国际化后的context 来获取资源
        AppCacheUtil.getInstance().setAppContext(context);

        //监听蓝牙变化
        bleStateListner(context);

        //监听App的前后台
        registerActivityLifecycleCallbacks(this);

        //键盘弹出和回收的监听
        KeyboardVisibilityObserver.getInstance().init(this);

        //崩溃上报
        crashReport();
    }

    @Override
    public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle bundle) {

    }

    @Override
    public void onActivityStarted(@NonNull Activity activity) {
        activityReferences++;
        if (activityReferences == 1 && !isActivityChangingConfigurations) {
            // App comes to the foreground
            Log.i("MyApplication", "onAppForegrounded");
            EventBus.getDefault().post(new MessageEvent(MessageEvent.APP_FOREGROUND, null));
        }
    }

    @Override
    public void onActivityStopped(@NonNull Activity activity) {
        activityReferences--;
        isActivityChangingConfigurations = activity.isChangingConfigurations();
        if (activityReferences == 0 && !isActivityChangingConfigurations) {
            // App goes to the background
            Log.i("MyApplication", "onAppBackgrounded");
            EventBus.getDefault().post(new MessageEvent(MessageEvent.APP_BACKGROUND, null));
        }
    }

    @Override
    public void onActivityResumed(@NonNull Activity activity) {

    }

    @Override
    public void onActivityPaused(@NonNull Activity activity) {

    }

    @Override
    public void onActivityDestroyed(@NonNull Activity activity) {

    }

    @Override
    public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle bundle) {

    }

    private void bleStateListner(Context context) {
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
        intentFilter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
        intentFilter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);

        registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();
                if(action != null){
                    boolean isConnect = false;
                    switch (action) {
                        case BluetoothAdapter.ACTION_STATE_CHANGED:
                            int blueState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, 0);
                            switch (blueState) {
                                case BluetoothAdapter.STATE_TURNING_ON:
                                    showToast(context, getString(R.string.ble_state_turning_on));
                                    break;
                                case BluetoothAdapter.STATE_ON:
                                    showToast(context, getString(R.string.ble_state_on));
                                    break;
                                case BluetoothAdapter.STATE_TURNING_OFF:
                                    showToast(context, getString(R.string.ble_state_turning_off));
                                    break;
                                case BluetoothAdapter.STATE_OFF:
                                    showToast(context, getString(R.string.ble_state_off));
                                    break;
                            }
                            break;

                        case BluetoothDevice.ACTION_ACL_CONNECTED:
//                            showToast("蓝牙设备已连接");
                            isConnect = true;
                            break;

                        case BluetoothDevice.ACTION_ACL_DISCONNECTED:
//                            showToast("蓝牙已断开！请确认发球机的工作状态");
                            break;
                    }

//                    if (isConnect && DFBleManager.getInstance().getBleDeviceBean() != null) {
//                        EventBus.getDefault().post(new MessageEvent(MessageEvent.BLE_CONNECT_STATE_SUCCESS, DFBleManager.getInstance().getBleDeviceBean()));
//                    }
//                    if (!isConnect) {
//                        EventBus.getDefault().post(new MessageEvent(MessageEvent.BLE_CONNECT_STATE_OTHER, ""));
//                    }
                }
            }
        }, intentFilter);
    }

    private void showToast(Context context,String msg) {
        if (context == null) {
            context = getApplicationContext();
        }
        ToastUtil.showToast(context, msg);
    }

    private void crashReport() {
        //bugly
        String appid = "cceed51837";
        CrashReport.initCrashReport(getApplicationContext(), appid, true);
    }
}
